package com.hackathon.chgk.taskloaders.impl;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.hackathon.chgk.data.dao.Task;
import com.hackathon.chgk.taskloaders.TaskLoader;
import com.hackathon.chgk.utils.NetworkMode;

import android.content.Context;
import android.util.Log;

public class TaskLoaderImpl implements TaskLoader {
	
	private String url;
	private NetworkMode networkMode;
	private Context context;
	private TaskLoader engine; 
	
	static final String CLASSNAME = TaskLoaderImpl.class.getName();
	
	public TaskLoaderImpl(String url, NetworkMode networkMode, Context context) {

		this.url = url;
		this.setNetworkMode(networkMode);
		this.context = context;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public NetworkMode getNetworkMode() {
		return networkMode;
	}

	public void setNetworkMode(NetworkMode networkMode) {		
		
		switch (networkMode) {
	        case OFFLINE:
	        	
	        	engine = new OfflineTaskLoaderImpl(); 
	            break;
	                
	        case ONLINE:
	        	
				try {
					engine = new OnlineTaskLoaderImpl(new URL(url));
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	            
	            break;
	                     
	        case MIXED:
	        	
	        	engine = new MixedTaskLoaderImpl();
	            break;        
		}
		this.networkMode = networkMode;
		Log.d(CLASSNAME, "Network mode is " + networkMode);
	}

	public Task getRandomTask() {
		
		return engine.getRandomTask();
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}	

	public List<Task> getRandomTasks(int count) throws Exception {
		// TODO Auto-generated method stub
		return engine.getRandomTasks(count);
	}

}
